package com.project.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import gz.ChargeDispensingService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(value = {"*"},allowCredentials = "true")
public class ChargeDispensingController {


    @Reference
    private ChargeDispensingService cs;


    /**
     * 查询出所有未就诊的所有患者
     * @param name
     * @param status
     * @return
     */
    @RequestMapping("sffy/getall")
    public List<Map<String,Object>> all(String name,Integer status){
        System.out.println("姓名："+name+"***状态："+status);
        List<Map<String,Object>> all = cs.AllChargeDis(name,status);
        System.out.println(all.isEmpty());
        return  all;
    }


    /**
     * 点击收费按钮获取对应患者id查询出该患者的信息
     * @param request
     * @return
     */
    @RequestMapping("pay/sf")
    public List<Map<String,Object>> Charge(HttpServletRequest request){
        String id = request.getParameter("id");
        Integer ids = Integer.valueOf(id);
        System.out.println("收费传过来的参数："+ids);
        List<Map<String,Object>> map = cs.Person_Charge(ids);
        System.out.println(map.isEmpty());
        return map;
    }


    /**
     * 如现金收费成功则修改患者的状态且新增记录到表中
     * @param request
     * @return
     */
    @RequestMapping("pay/goadd")
    public Map<String,Object> update_status(HttpServletRequest request){
        String id = request.getParameter("id");
        String m = request.getParameter("money");
        String r = request.getParameter("registration_id");
        String p = request.getParameter("patient_id");
        String h = request.getParameter("history_id");
        String order_code = request.getParameter("order_code");
        Double money = Double.parseDouble(m);
        Integer ids = Integer.valueOf(id);
        Integer registration_id = Integer.valueOf(r);
        Integer patient_id = Integer.valueOf(p);
        Integer history_id = Integer.valueOf(h);
//        Integer order_code = Integer.valueOf(o);
        System.out.println("现金收费传过来的参数："+ids+"金额："+money);
        int i = cs.update_status(ids);
        int s = cs.money_record_all(history_id,registration_id,patient_id,money,order_code);
        int k = cs.select_pkgId(order_code);
        int d = cs.money_record(k,money);
        System.out.println(i+s+d+"行受影响");
        System.out.println("pkgid:"+k);
        Map<String,Object> map = new HashMap<>();
        if(i>0&&s>0&&d>0){
            map.put("msg","true");
        }else{
            map.put("msg","false");
        }
        return  map;
    }


    /**
     * 如退款修改则修改患者的状态且新增记录到表中
     * @param request
     * @return
     */
    @RequestMapping("pay/tuikuan")
    public Map<String,Object> update_tuikuan(HttpServletRequest request){
        String id = request.getParameter("id");
        String pkg_id = request.getParameter("pkg_id");
        String m = request.getParameter("money");
        Integer ids = Integer.valueOf(id);
        Integer pkg = Integer.valueOf(pkg_id);
        Double money = Double.parseDouble(m);
        System.out.println("退款传过来的参数："+ids);
        int i = cs.update_tuikuan(ids);
        int s = cs.insert_tuikuan(ids,money,pkg);
        System.out.println(i+s+"行受影响");
        Map<String,Object> map = new HashMap<>();
        if(i>0&&s>0){
            map.put("msg","true");
        }else{
            map.put("msg","false");
        }
        return  map;
    }


    /**
     * 如微信收费成功则修改患者的状态且新增记录到表中
     * @param request
     * @return
     */
    @RequestMapping("pay/gopay")
    public Map<String,Object> update_weixin(HttpServletRequest request){
        String id = request.getParameter("id");
        String m = request.getParameter("money");
        String r = request.getParameter("registration_id");
        String p = request.getParameter("patient_id");
        String h = request.getParameter("history_id");
        String order_code = request.getParameter("order_code");
        Integer ids = Integer.valueOf(id);
        Double money = Double.parseDouble(m);
        Integer registration_id = Integer.valueOf(r);
        Integer patient_id = Integer.valueOf(p);
        Integer history_id = Integer.valueOf(h);
//        Integer order_code = Integer.valueOf(o);
        System.out.println("微信传过来的参数："+ids+"金额："+money);
        int i = cs.update_status(ids);
        int s = cs.weixin_record_all(history_id,registration_id,patient_id,money,order_code);
        int k = cs.select_pkgId(order_code);
        int w = cs.weixin_record(k,money);
        System.out.println(i+s+w+"行受影响");
        System.out.println("pkgid:"+k);
        Map<String,Object> map = new HashMap<>();
        if(i>0&&s>0&&w>0){
            map.put("msg","true");
        }else{
            map.put("msg","false");
        }
        return  map;
    }


    /**
     * 收费成功执行发药的功能，点击发药按钮修改患者的状态为完成交易
     * @param request
     * @return
     */
    @RequestMapping("pay/fayao")
    public Map<String,Object> fayao(HttpServletRequest request){
        String id = request.getParameter("id");
        System.out.println("发药传过来的参数："+id);
        Integer i = Integer.parseInt(id);
        int s = cs.update_ok(i);
        System.out.println(s+"行受影响");
        Map<String,Object> map = new HashMap<>();
        if(s>0){
            map.put("msg","true");
        }else{
            map.put("msg","false");
        }
        return  map;
    }
}
